import { DirectiveBinding } from "vue";

function checkPermission(el: HTMLElement, binding: DirectiveBinding) {
  // 获取绑定的值
  const { value } = binding;
  // 获取功能指令
  const points: any = [];
  console.log(points);
  // 检查指令
  if (value && value instanceof Array) {
    const hasPermission = points.some((point: string) => {
      return value.includes(point);
    });
    // 如果没有权限就移除元素
    if (!hasPermission) {
      el.parentNode && el.parentNode.removeChild(el);
    }
  } else {
    throw new Error('v-permission value is ["admin", editor]');
  }
}

export default {
  // 绑定元素被挂载时触发
  mounted(el: HTMLElement, binding: DirectiveBinding) {
    checkPermission(el, binding);
  },
  // 绑定元素更新时触发
  updated(el: HTMLElement, binding: DirectiveBinding) {
    checkPermission(el, binding);
  },
};
